package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class DishServiceImpl implements DishService {
    /**
     * 新增菜品
     */
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setMealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 新增菜品
     */
    @Override
    public void add(DishDTO dto) {
        //向菜品表插入数据
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        dishMapper.insert(dish);
        Long id=dish.getId();
        //向口味表插入数据
        List<DishFlavor> dishFlavors=dto.getFlavors();
        if(!dishFlavors.isEmpty()){
            dishFlavors.forEach(dishFlavor->dishFlavor.setDishId(id));
            dishFlavorMapper.insertWithFlavor(dto.getFlavors());//可以直接把集合放进去，批量插入
        }




    }


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(DishPageQueryDTO dishPageQueryDTO) {
/*        //开启分页
        Page<Object> page = PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        //分页操作可以获得total，但是list里面的内容需要自己查
        List<Dish>list=dishMapper.pageQuery(dishPageQueryDTO);
        Long total=page.getTotal();
        PageResult pageResult = new PageResult(total,list);
        return pageResult;*/
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO>page=dishMapper.pageQuery(dishPageQueryDTO);
        PageResult pageResult=new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page);
        return pageResult;
    }



    /**
     * 起售和禁售
     */
    public void startOrStop(Long status,Integer id){
        dishMapper.startOrstop(status,id);
    }


    /**
     * 根据id查询菜品和对应的口味
     * @param id
     * @return
     */
    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //查询菜品数据
        Dish dish=dishMapper.selectById(id);
        //查口味
        List<DishFlavor>dishFlavor=dishMapper.selectFlavorById(id);
        DishVO dishVO=new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }

    @Override
    public void updateDishWithFlavor(DishDTO dishDTO) {
        //修改菜品基本信息
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.update_dish(dish);
        Long dish_id=dish.getId();
        //删除原有的口味数据
        dishFlavorMapper.deleteFlavorById(dishDTO.getId());
        //重新插入口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> dishFlavor.setDishId(dish_id));
        // 在 DishServiceImpl 类的 updateDishWithFlavor 方法中（或调用 insertWithFlavor 的地方）
        if (flavors.size()==0) {
            // 集合为空，无需执行插入操作，可做相应日志记录或直接返回
            log.info("集合为空");
        }
// 集合不为空，执行插入
        dishFlavorMapper.insertWithFlavor(flavors);

    }

    @Override
    public void delete(List<Long> ids) {
        for (Long id : ids) {
            //判断是否起售
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断是否被套餐关联
        List<Long> setmeals=setMealDishMapper.selectById(ids);
        if(setmeals.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        //其他的都可以删
        dishMapper.delete(ids);
    }

    @Override
    public List<Dish> listById(Integer categoryId) {
        List<Dish> list=dishMapper.listById(categoryId);
        return list;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }



}
